<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统计日期" prop="selectDate">
        <el-date-picker clearable
          v-model="queryParams.selectDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择统计日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div ref="divTable" :style="'height:' + divHeight + 'px'">
      <el-table v-loading="loading" :data="indicatorsFileList" @selection-change="handleSelectionChange">
        <el-table-column label="文件编码" align="center" width="250px" prop="id"/>
        <el-table-column label="店铺" align="center" prop="storeName" :show-overflow-tooltip='true'/>
        <el-table-column label="文件名称" align="center" prop="fileName" :show-overflow-tooltip='true'/>
        <el-table-column label="统计日期" align="center" prop="selectDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.selectDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="文件类型" align="center" prop="type" :show-overflow-tooltip='true'>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 1">店铺</el-tag>
            <el-tag v-else-if="scope.row.type === 2">商品</el-tag>
            <el-tag v-else-if="scope.row.type === 3">全站</el-tag>
            <el-tag v-else-if="scope.row.type === 4">无界</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="绝对路径" align="center" prop="absolutePath" :show-overflow-tooltip='true'/>
        <el-table-column label="文件哈希值" align="center" prop="fileHash" :show-overflow-tooltip='true'/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['business:indicatorsFile:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :pageRefs="this.$refs"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
    </div>
    <!-- 添加或修改指标数据文件管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺id" />
        </el-form-item>
        <el-form-item label="上传日期" prop="selectDate">
          <el-date-picker clearable
            v-model="form.selectDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上传日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="绝对路径" prop="absolutePath">
          <el-input v-model="form.absolutePath" placeholder="请输入绝对路径" />
        </el-form-item>
        <el-form-item label="文件哈希值" prop="fileHash">
          <el-input v-model="form.fileHash" placeholder="请输入文件哈希值" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listIndicatorsFile,
  getIndicatorsFile,
  delIndicatorsFile,
  addIndicatorsFile,
  updateIndicatorsFile,
  exportExcel,
  deleteFileOrData
} from "@/api/business/indicatorsFile";
import ResizeMixin from "@/layout/mixin/ResizeHandler";
import {parseTime} from "../../../utils/basic";

export default {
  name: "IndicatorsFile",
  mixins: [ResizeMixin],
  data() {
    return {
      // div高度
      divHeight: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 指标数据文件管理表格数据
      indicatorsFileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        selectDate: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      },
      typeOptions: [{
        value: 1,
        label: '店铺'
      }, {
        value: 2,
        label: '商品'
      }, {
        value: 3,
        label: '全站'
      }, {
        value: 4,
        label: '无界'
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询指标数据文件管理列表 */
    getList() {
      this.loading = true;
      listIndicatorsFile(this.queryParams).then(response => {
        this.indicatorsFileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeId: null,
        storeName: null,
        selectDate: null,
        absolutePath: null,
        fileHash: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        status: null,
        remark: null
      };
      this.$resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$resetForm("queryForm");
      this.queryParams.storeName=null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加指标数据文件管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIndicatorsFile(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改指标数据文件管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateIndicatorsFile(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIndicatorsFile(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除指标数据文件管理编号为"' + row.id + '"的数据项？').then(function() {
        return deleteFileOrData({id:row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
      this.deleteVisible=false;
    },
    /** 导出按钮操作 */
    handleExport() {
      exportExcel(this.queryParams)
    }
  }
};
</script>
